﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using MapControl.NativeApi;
using System.IO;
using System.Drawing;

namespace MapControl
{
    public class TransparentImage : IDisposable
    {
        private IImage image;
        private IImagingFactory factory = (IImagingFactory)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("327ABDA8-072B-11D3-9D7B-0000F81EF32E")));
        private Bitmap imageBitmap;

        internal IImage Image
        {
            get { return image; }
        }

        public Image ImageBitmap
        {
            get { return imageBitmap; }
        }

        public TransparentImage(string fileName)
        {
            factory.CreateImageFromFile(fileName, out image);
            imageBitmap = new Bitmap(fileName);
        }

        public TransparentImage(byte[] buffer, uint length)
        {
            factory.CreateImageFromBuffer(buffer, length, BufferDisposalFlag.BufferDisposalFlagNone, out image);
        }

        public TransparentImage(MemoryStream stream)
        {
            factory.CreateImageFromBuffer(stream.GetBuffer(), (uint)stream.Length, BufferDisposalFlag.BufferDisposalFlagNone, out image);
            imageBitmap = new Bitmap(stream);
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (imageBitmap != null)
                imageBitmap.Dispose();
        }

        #endregion
    }
}
